// pages/index/index.js
import wifi from './../../utils/SchoolWiFi'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    checking: 'false',
    netWorkType: '',
    tip: '初始化',
    // 是否存在本地库中
    isSave: false,
  },
  _refreshTip(value) {
    this.setData({
      tip: value
    })
  },
  start() {
    console.log(wifi)
    const that = this
    // 判断当前网络环境
    if (that.data.netWorkType == 'wifi') {
      wx.startWifi({
        complete: (res) => {
          if (res.errCode == 0) {
            that._refreshTip(that.data.tip + '....完成\nWiFi已开启')
            wx.onWifiConnected(function (res) {
              console.log(res)
              that._refreshTip(that.data.tip + '...完成\nWiFi正在检测中')
              that._refreshTip(that.data.tip + '...完成\n获取WiFi名称中')
              that._refreshTip(that.data.tip + res.wifi.SSID + '...完成\n获取WiFi物理地址中')
              that._refreshTip(that.data.tip + res.wifi.BSSID + '...完成\n获取WiFi信号强度')
              let Strange = res.wifi.signalStrength > 90 ? '超强' : res.wifi.signalStrength > 70 ? '强' : res.wifi.signalStrength > 50 ? '较强' : res.wifi.signalStrength > 30 ? '弱' : '很弱！不建议连接'
              that._refreshTip(that.data.tip + res.wifi.signalStrength + '...完成\n当前网络评估')
              that._refreshTip(that.data.tip + Strange + '...完成\n检验是否为本校WIFI')
              for (let i in wifi.wifi) {
                console.log(res.wifi.BSSID == i, res.wifi.BSSID, i, wifi.wifi[i])
                if (res.wifi.BSSID == i) {
                  that.data.isSave = true
                  that._refreshTip(that.data.tip + '...完成\n当前所连WiFi' + wifi.wifi[i])
                  break
                }
              }
              if (!that.data.isSave) {
                that._refreshTip(that.data.tip + '...完成\n当前所连WiFi非本校WIFI')
              }
              wx.request({
                url: 'https://pv.sohu.com/cityjson?ie=utf-8',
                success: function (res) {
                  let returnCitySN = JSON.parse(/\{.*\}/.exec(res.data)[0])
                  that._refreshTip(that.data.tip + '\n' + returnCitySN.cip + returnCitySN.cname+'...')
                  that._refreshTip(that.data.tip + "\n正在检测是否与学校外网一致...")
                  if(returnCitySN.cip == wifi.external_ip[0]){
                    that._refreshTip(that.data.tip + "\n恭喜！通过验证")
                  }
                  else{
                    that._refreshTip(that.data.tip + "\n警报！警报！小可爱你的网络可能非本校WIFI，请注意您的网络安全！")
                    that.setData({
                      show:true
                    })
                  }
                }
              })
            })
          }
          else {
            that._refreshTip('WIFI好像出了点问题，请检查手机WiFi开关后再试！')
          }
        },
      })
    }
    else {
      that.setData({
        tip: '有点不开心，打开WiFi再试试吧！'
      })
    }
    this.setData({
      tip: '初始化'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getNetworkType({
      success: function (res) {
        that.setData({
          netWorkType: res.networkType
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}) 